import { beforeAll, describe, test, expect } from 'vitest';
import { FileUtils } from '~/core/extensions';
import { execSync } from 'child_process';

let rowArr: Array<Array<string>> = []

beforeAll(() => {
    execSync('npm run build', { encoding: 'utf-8' });
    execSync('node dist/index.js', { encoding: 'utf-8' });
    let result = FileUtils.readTextFile('result.json');
    result = result.slice(result.indexOf('\n'));
    rowArr = JSON.parse(result).rows;
})

describe('AvoidFrequentStateUpdateTest', () => {
    test('AvoidFrequentStateUpdateTest_001', () => {
        const expectFile = 'AvoidFrequentStateUpdateReport1.ets';
        const expectPair = [['41', '27'], ['43', '28']];
        let rows = rowArr.filter((row: Array<String>) => {
            return row[0].includes(expectFile);
        });
        expectPair.forEach((pair: Array<string>) => {
            let flag = false;
            rows.forEach((row: Array<String>) => {
                let path = row[2].split(' -> ');
                if (path.includes(pair[0]) && path.includes(pair[1])) {
                    flag = true;
                }
            });
            expect(flag).toBeTruthy();
        });
    });

    test('AvoidFrequentStateUpdateTest_002', () => {
        const expectFile = 'AvoidFrequentStateUpdateReport2.ets';
        const expectPair = [['35', '27'], ['38', '28']];
        let rows = rowArr.filter((row: Array<String>) => {
            return row[0].includes(expectFile);
        });
        expectPair.forEach((pair: Array<string>) => {
            let flag = false;
            rows.forEach((row: Array<String>) => {
                let path = row[2].split(' -> ');
                if (path.includes(pair[0]) && path.includes(pair[1])) {
                    flag = true;
                }
            });
            expect(flag).toBeTruthy();
        });
    });

    test('AvoidFrequentStateUpdateTest_003', () => {
        const expectFile = 'AvoidFrequentStateUpdateReport3.ets';
        const expectPair = [['38', '27']];
        let rows = rowArr.filter((row: Array<String>) => {
            return row[0].includes(expectFile);
        });
        expectPair.forEach((pair: Array<string>) => {
            let flag = false;
            rows.forEach((row: Array<String>) => {
                let path = row[2].split(' -> ');
                if (path.includes(pair[0]) && path.includes(pair[1])) {
                    flag = true;
                }
            });
            expect(flag).toBeTruthy();
        });
    });

    test('AvoidFrequentStateUpdateTest_004', () => {
        const expectFile = 'AvoidFrequentStateUpdateNoReport1.ets';
        let rows = rowArr.filter((row: Array<String>) => {
            return row[0].includes(expectFile);
        });
        expect(rows.length).toBe(0);
    });

    test('AvoidFrequentStateUpdateTest_005', () => {
        const expectFile = 'AvoidFrequentStateUpdateNoReport2.ets';
        let rows = rowArr.filter((row: Array<String>) => {
            return row[0].includes(expectFile);
        });
        expect(rows.length).toBe(0);
    });

})
